<template> </template>

<script>
import bus from './bus';
import { getUserData } from '@/utils/IndexedDB.js';
export default {
	name: 'timer',
	data() {
		return {
			timer: '',
			totalIntval: 1000,
			timerTodo: [
				{
					name: 'Warning',
					eventName: 'timerWarning',
					interval: 1000*10,
					intervalNow: 0,
				},
			],
		};
	},
	created() {
		bus.$on('timerNewTodo', (msg) => {
			this.timerTodoUpdate(msg);
		});

		this.initTotalIntval();
	},
	computed: {
		
	},
	mounted() {},
	watch: {
		
	},

	methods: {
		async initTotalIntval() {
			this.timer = setInterval(() => {
				if (this.timerTodo.length > 0) {
					this.timerTodo.forEach((element) => {
						element.intervalNow += this.totalIntval;

						if (element.intervalNow >= element.interval) {
							element.intervalNow = 0;
							setTimeout(
								this.timerTodoEmit,
								0,
								element.eventName
							);
						}
					});
				}
			}, this.totalIntval);
		},
		timerTodoUpdate(timerNewTodo) {
			this.timerTodo = this.timerTodo.filter(({ name }) => {
				return name !== timerNewTodo.name;
			});

			if (timerNewTodo.hasOwnProperty('eventName')) {
				this.timerTodo.push(timerNewTodo);
			}
		},
		timerTodoEmit(eventName) {
			bus.$emit(eventName, { isGetData: true });
		},
	},
	beforeDestroy() {
		clearInterval(this.timer);
	},
};
</script>

<style scoped></style>
